import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:life_milestones/MS-global-url.dart';
import 'package:life_milestones/MS-request.dart';
import 'package:life_milestones/controller/MS-global-user-controller.dart';
import 'package:life_milestones/controller/MS-home-controller.dart';
import 'package:life_milestones/controller/MS-mile-stone-controller.dart';
import 'package:life_milestones/util/MS-toast-util.dart';
import 'package:provider/provider.dart';

import '../util/MS-log-util.dart';
class MSSettingPage extends StatefulWidget{
  const MSSettingPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _MSSettingPageState();
  }

}

class _MSSettingPageState extends State<MSSettingPage>{
  bool isLogoutDoing = false;
  bool isDeleteDoing = false;
  Future<bool> logout()async{
    var response = await MSRequest().post(MSGlobalUrl.logout);
    if(response["code"] == 0 && response["data"] == "Success"){
      return true;
    }
    return false;
  }

  Future<bool> deleteAccount()async{
    var response = await MSRequest().post(MSGlobalUrl.deleteAccount);
    if(response["code"] == 0 && response["data"]){
      return true;
    }
    return false;
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<MSGlobalUserController>(builder: (BuildContext context,MSGlobalUserController controller,Widget? child){
      return Scaffold(
          body: SizedBox.expand(child:DecoratedBox(
              decoration: const BoxDecoration(
                  image: DecorationImage(
                      image: AssetImage("images/mine-common-bg.png")
                  )
              ),
              child: Column(
                children: [
                  Container(height: ScreenUtil().statusBarHeight + 57.w,width: double.infinity,
                    padding: EdgeInsets.only(top: ScreenUtil().statusBarHeight,left: 24.w,right: 24.w),
                    child: Stack(
                      children: [
                        Align(alignment: Alignment.centerLeft,child: InkWell(
                          onTap: (){
                            Navigator.of(context).pop();
                          },
                          child: Image.asset("images/back.png",width: 25.w,fit: BoxFit.fitWidth,),),),
                        Align(alignment: Alignment.center,child: Text("Setting",style: TextStyle(fontSize: 20.sp,color: Color(0xFF333333),fontWeight: FontWeight.w500),),)

                      ],
                    ),
                  ),
                  Padding(padding: EdgeInsets.only(left: 40.w,right: 36.w,top: 44.w),child: Container(
                    height: 56.w,
                    width: double.infinity,
                    decoration: BoxDecoration(
                        border: Border(
                            bottom: BorderSide(color: Color(0x40333333),width: 1.w)
                        )
                    ),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.center,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text("id：${controller.user!.userId}",style: TextStyle(fontSize: 16.sp,color: Color(0xFF333333),fontWeight: FontWeight.w500),),
                        InkWell(
                            onTap: (){
                              Clipboard.setData(ClipboardData(text: controller.user!.userId!));
                              showToast("Copy Successfully");
                            },
                            child: Image.asset("images/copy.png",width: 25.w,fit: BoxFit.fitWidth,))
                      ],
                    ),
                  ),),

                  Padding(padding: EdgeInsets.only(left: 40.w,right: 36.w),child: InkWell(
                    onTap: ()async{
                      if(isLogoutDoing){
                        return;
                      }
                      isLogoutDoing = true;
                      MSLog.addEvent(page:MSLogPages.Logout);
                      logout().then((value){
                        if(value){
                        Navigator.pushNamedAndRemoveUntil(context,"login",ModalRoute.withName("login")).then((value){
                          Future.wait([MSHomeController().onLogout(),MSMileStoneController().onLogout()].iterator as Iterable<Future>).then((value){
                            MSGlobalUserController().onLogout();
                          });
                        });
                        }else{
                          isLogoutDoing = false;
                        }
                      });

                    },
                    child: Container(
                      height: 56.w,
                      width: double.infinity,
                      decoration: BoxDecoration(
                          border: Border(
                              bottom: BorderSide(color: const Color(0x40333333),width: 1.w)
                          )
                      ),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text("Logout",style: TextStyle(fontSize: 16.sp,color: Color(0xFF333333),fontWeight: FontWeight.w500),),
                          Text("")

                        ],
                      ),
                    ),
                  ),),
                  Padding(padding: EdgeInsets.only(left: 40.w,right: 36.w),child: InkWell(
                    onTap: (){
                      if(isDeleteDoing){
                        return;
                      }
                      isDeleteDoing = true;
                      MSLog.addEvent(page:MSLogPages.DeleteAccount);
                      deleteAccount().then((value){
                        if(value){
                          Navigator.pushNamedAndRemoveUntil(context,"login",ModalRoute.withName("login")).then((value){
                            Future.wait([MSHomeController().onDelete(),MSMileStoneController().onDelete()].iterator as Iterable<Future>).then((value){
                              MSGlobalUserController().onLogout();
                            });
                          });
                          isDeleteDoing = false;
                        }else {
                        isDeleteDoing = false;
                        }
                      });
                    },
                    child: SizedBox(
                      height: 56.w,
                      width: double.infinity,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text("Delete account",style: TextStyle(fontSize: 16.sp,color: Color(0xFF333333),fontWeight: FontWeight.w500),),
                          const Text("")

                        ],
                      ),
                    ),
                  ),),
                ],
              ))));
    });
  }

}